<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Admin\Sys;

use App\Controller\AbstractController;
use App\Middleware\AuthMiddleware;
use App\Model\Article;
use App\Model\ArticleTag;
use App\Model\Menu;
use App\Model\Role;
use App\Model\RoleMenu;
use App\Model\Tag;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Annotation\RequestMapping;

#[Middlewares([AuthMiddleware::class])]
#[Controller]
class RoleController extends AbstractController
{
    #[GetMapping(path: '/sys/role/all')]
    public function all()
    {
        $list = Role::query()->get();
        return success($list);
    }

    #[GetMapping(path: '/sys/role')]
    public function list()
    {
        $pageNum = $this->request->input('pageNum', 1);
        $pageSize = $this->request->input('pageSize', 10);
        $name = $this->request->input('name', '');
        $list = Role::query()->where('name', 'like', '%' . $name . '%')->forPage($pageNum, $pageSize)->get();
        $data['searchCount'] = true;
        $data['current'] = (int)$pageNum;
        $data['size'] = (int)$pageSize;
        $data['total'] = Role::query()->count();
        $data['pages'] = ceil($data['total'] / $pageSize);
        $data['records'] = $list;
        return success($data);
    }

    #[RequestMapping(path: '/sys/role', methods: ['PUT', 'POST'])]
    public function update()
    {
        $data = $this->request->all();
        if (empty($data['id'])) {
            Role::query()->create($data);
        } else {
            Role::query()->where('id', $data['id'])->update([
                'code' => $data['code'],
                'name' => $data['name'],
                'remarks' => $data['remarks'],
            ]);
        }
        return success();
    }


    #[DeleteMapping(path: '/sys/role/delete/{ids}')]
    public function delete($ids)
    {
        try {
            $idArr = explode(',', $ids);
            Db::beginTransaction();
            Role::query()->where('id', $idArr)->delete();
            RoleMenu::query()->where('role_id', $idArr)->delete();
            Db::commit();
            return success();
        } catch (\Throwable $e) {
            Db::rollBack();
            logInfo('delete', $e->getMessage(), 'role');
            return fail(201, $e->getMessage());
        }

    }

    #[GetMapping(path: '/sys/role/menus/{id}')]
    public function menu($id)
    {
        $role = Role::query()->where('id', $id)->first();
        if ($role) {
            $menuIds = RoleMenu::query()->where('role_id', $id)->get()->pluck('menu_id');
            $list = Menu::query()->whereIn('id', $menuIds)->get();
            return success($list);
        }
    }

    #[PutMapping(path: '/sys/role/menus/{id}')]
    public function menuUpdate($id)
    {
        $menuIds = $this->request->all();
        try {
            Db::beginTransaction();
            RoleMenu::query()->where('role_id', $id)->delete();
            foreach ($menuIds as $menuId) {
                RoleMenu::query()->create([
                    'role_id' => $id,
                    'menu_id' => $menuId,
                ]);
            }
            Db::commit();
            return success();
        } catch (\Throwable $e) {
            Db::rollBack();
            logInfo('menuUpdate', $e->getMessage(), 'role');
            return fail(201, $e->getMessage());
        }

    }
}
